package com.its;

import com.its.consumer.service.api.TestService;
import com.its.provider.service.api.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author Xiaxiaopeng
 * @description 功能描述
 * @create 2023/5/11 10:39
 */
@RestController
@RequestMapping("test")
@Slf4j
public class TestController {
    @DubboReference(group = "group1",version = "1.0", check = false)
    private TestService testService;

    @DubboReference(group = "group2",version = "2.0", check = false)
    private TestService testService2;

    @GetMapping("/sayHello")
    public String sayHello(@RequestParam String content) {
        log.info(Thread.currentThread().getName() + ", " + Thread.currentThread().getId());
        String rpcResult = testService.testHi("-- v1") + "." + testService2.testHi("-- v2");
        return rpcResult;
    }

    @GetMapping("/sayHello2")
    public String sayHello2(@RequestParam String content) throws ExecutionException, InterruptedException {
        log.info(Thread.currentThread().getName() + ", " + Thread.currentThread().getId());

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> testService.testHi("-- v1"));
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> testService2.testHi("-- v2"));
        String rpcResult = future1.get() + "." + future2.get();
        return rpcResult;
    }
}
